function NumFormat(intNum){
	return (intNum < 10)?("0" + intNum.toString()):intNum.toString();
}

function clearTextField(){
	var i = 0, j = 0, intNum = 0;
	for(i =1; i <= 5; i++)	{
		for (j = 0; j <= 4; j++){
			intNum = 5*j + i;
			if (intNum > 21) continue;
			if(null != document.ShowOddsForm["Odds1_"+intNum.toString()]){
				document.ShowOddsForm["Odds1_"+intNum.toString()].value = "";
			}
		}
	}
	if(parseInt(isSpecial) >= 5 && parseInt(isSpecial) <= 8)return;
	if(null != document.ShowOddsForm["Odds2_0"])document.ShowOddsForm["Odds2_0"].value = "";
	if(null != document.ShowOddsForm["Odds2_1"])document.ShowOddsForm["Odds2_1"].value = "";
	if(null != document.ShowOddsForm["Odds3_0"])document.ShowOddsForm["Odds3_0"].value = "";
	if(null != document.ShowOddsForm["Odds3_1"])document.ShowOddsForm["Odds3_1"].value = "";
	if(null != document.ShowOddsForm["Odds4_0"])document.ShowOddsForm["Odds4_0"].value = "";
	if(null != document.ShowOddsForm["Odds4_1"])document.ShowOddsForm["Odds4_1"].value = "";
}

function GetWagerTable(){
	if(document.getElementById("OddsID1") && document.getElementById("OddsID49")){
		InsertOdds();
		return;
	}

	var objWTable = document.getElementById("wager_table"), i = 0, j = 0, intNum = 0, strNum = "", strDisabled = "", StopClass = "mbline";
	var ArrGame = eval("parent.Game"+isSpecial);
	var ArrStake = new Array();

	with(objWTable)	{
		for(i =1; i <= 5; i++)	{
			for (j = 0; j <= 4; j++){
				intNum = 5*j + i;
				if (intNum > 21) continue;
				if(null != document.ShowOddsForm["Odds1_"+intNum.toString()]){
					ArrStake[intNum] = document.ShowOddsForm["Odds1_"+intNum.toString()].value;
				}else{
					ArrStake[intNum] = "";
				}
			}
		}
		while(rows.length > 1)	deleteRow(rows.length - 1);
			
		for(i = 1; i <= 5; i++)	{
			newTR = insertRow(rows.length);
			newTR.className = (i % 2 == 1)?"mcen":"mcen_ev";
			with(newTR){
				var intCellNo = 0;
				for (j = 0; j <= 4; j++){
					intNum = 5*j + i;
					strNum = NumFormat(intNum);
										
					if (intNum == 25) {
						newTD = insertCell(intCellNo);
						newTD.colSpan = 3;
						newTD.innerHTML = "<input type=\"button\" value=\""+btnSend+"\" onclick=\"FinalCheck();\" class='zabottonR'><input type=\"reset\" value=\""+btnReset+"\" class='zabotton'>";
						newTD.className = "mbline";
						intCellNo++;
					}else if (ArrGame[intNum] == null) {
						newTD = insertCell(intCellNo);
						newTD.colSpan = 3;
						newTD.innerHTML = "&nbsp;";
						newTD.className = "mbline";
						intCellNo++;
					}else{
						newTD = insertCell(intCellNo);
						newTD.className = "mcen_ev_th";
						newTD.innerHTML = strNum;
						intCellNo++;

						newTD = insertCell(intCellNo);
						newTD.className = "mbline_ratio";
						newTD.id = "OddsID"+intNum.toString();
						newTD.innerHTML = ArrGame[intNum][5];
						intCellNo++;

						newTD = insertCell(intCellNo);
						newTD.id = "OddsValue"+intNum.toString();
						newTD.className = StopClass;
						newTD.innerHTML = "<input name=\"Odds1_"+intNum.toString()+"\" value=\""+ArrStake[intNum]+"\" type=\"text\" class=\"zamoney\"  onkeypress=\"return CheckNumKey()\" onKeyUp=\"return Calculate()\" onchange=\"Calculate()\" "+strDisabled+">";
						intCellNo++;
					}
				}
			}
		}
	}
	InsertOdds();
}

function InsertOdds(){
	var ArrGame = eval("parent.Game"+isSpecial);
	var newTD1, newTD2, newTD3, intNum = 0;

	for(i =1; i <= 5; i++)	{
		for (j = 0; j <= 4; j++){
			intNum = 5*j + i;
			newTD1 = document.getElementById("OddsID"+intNum.toString());
			newTD3 = document.getElementById("OddsValue"+intNum.toString());
			if (ArrGame[intNum] == null || intNum > 21) {
				continue;
			}else if(ArrGame[intNum][6] == "1"){
				newTD1.className = "mbline_ratio_stop";
				newTD3.className = "mbline_stop";
			}else{
				newTD1.className = "mbline_ratio";
				newTD3.className = "mbline";
			}
			newTD1.innerHTML = ArrGame[intNum][5];
		}
	}
}

function CheckNumKey(){
	if(event.keyCode == 13) return true;
	if((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode > 95 || event.keyCode < 106)){
		alert(notNumber);
		return false;
	}
}

function OtherCheck(objGold, flOdds, intBID, intNum){
	if(intBID == 1)intNum -= 1;
	if(parseInt(isSpecial) > 20)intBID = parseInt(isSpecial);
	//下注金額必須為數值
	if(isNaN(objGold.value) == true){
		objGold.focus();
		alert(notNumber);
		return false;
	}
	//不能低於最低下注金額
	if(eval(objGold.value) < chkMinStake[intBID]){
		objGold.focus();
		alert(underMinStake+chkMinStake[intBID].toString());
		return false;
	}
	//不能超過本期單注限額
	if(eval(objGold.value) > chkLottoMaxBetLimitValue[intBID]){
		objGold.focus();
		alert(overLottoMaxBetLimitValue+chkLottoMaxBetLimitValue[intBID]);
		return false;
	}
	//不能超過單注限額
	if(eval(objGold.value) > chkMaxBetLimitValue[intBID]){
		objGold.focus();
		alert(overBetLimit + chkMaxBetLimitValue[intBID]);
		return false;
	}
	//不能超過單場限額
	if(eval(objGold.value) > (chkMaxEventLimitValue[intBID] - arrChildEveLimitUsed[intBID][intNum])){
		objGold.focus();
		alert(overEventLimit + (chkMaxEventLimitValue[intBID] - arrChildEveLimitUsed[intBID][intNum]).toString());
		return false;
	}
	//不能超過最高派彩限額
	if((eval(objGold.value)*eval(flOdds)) > chkMaxPayOff[intBID]){
		objGold.focus();
		alert(overMaxPayOff+chkMaxPayOff[intBID]);
		return false;
	} 
}

function FinalCheck(){
	var objGold, intBID = 0, intNum = 0, intTotal = 0, strWager = "", MaxBID = 3, intBID2 = 0;
	var ArrGame = new Array("", new Array(), new Array(), new Array(), new Array());
	var arrB = new Array("", new Array(1, 21), new Array(0, 1), new Array(0, 1), new Array(0, 1));
	//alert(isSpecial);
	if(isSpecial == "1"){
		ArrGame[1] = parent["Game"+isSpecial];
		ArrGame[2] = parent.Game2;
		ArrGame[3] = parent.Game3;
		ArrGame[4] = parent.Game4;
		MaxBID = 4;
	}else{
		ArrGame[1] = eval("parent.Game"+isSpecial);
		MaxBID = 1;
	}
	for(intBID = 1; intBID <= MaxBID; intBID++){
		intBID2 = intBID;
		for(intNum = arrB[intBID][0]; intNum <= arrB[intBID][1]; intNum++){
			objGold = document.ShowOddsForm["Odds" + intBID2.toString() + "_" + intNum.toString()];
			if (OtherCheck(objGold, ArrGame[intBID][intNum][5], intBID2, intNum) == false) return;
			if(objGold.value == "")continue;
			strWager += intNum.toString() + "#" + ArrGame[intBID][intNum][4] + "#" + objGold.value + "#";
			strWager += ArrGame[intBID][intNum][2] +"@";
			intTotal += parseInt(objGold.value);
		}
	}
	//請填入下注金額！
	if (intTotal == 0)	{
		alert(noMoney);
		return false;
	}
	//總金額不能超過信用餘額
	if (intTotal > chkCredit) {
		alert(overCredit+chkCredit);
		return false;
	}
	//確定下注？
	if (!confirm(sureStake))	return false;
	document.CommonWagerForm.WagerContent.value = strWager;
	document.CommonWagerForm.submit();
	document.GetOddsForm.submit();
}

function Calculate(){
	var intTotal = 0, objGold, arrB = new Array("", new Array(1, 21), new Array(0, 1), new Array(0, 1), new Array(0, 1));
	var MaxBID = 1;
	if(isSpecial == "1")MaxBID = 4;
	for(intBID = 1; intBID <= MaxBID; intBID++){
		for(intNum = arrB[intBID][0]; intNum <= arrB[intBID][1]; intNum++){
			objGold = document.ShowOddsForm["Odds" + intBID.toString() + "_" + intNum.toString()];
			if(objGold.value == "")continue;
			intTotal += parseInt(objGold.value);
		}
	}
	//document.getElementById("StakeMoney").innerHTML = intTotal;
}

function CountRefresh(){
	var intGT = 0, intGT2 = 0, strGTm = "", strGTs = "";
	if(intRefreshSec >= RefreshSec)isRefresh = true;
	intGT = parseInt(parent.strEndGameTime);
	if(intGT < 0)intGT = 0;
	intGT2 = (intGT > 900)?900:intGT;
	strGTm = (Math.floor(intGT2 / 60)).toString();
	if(strGTm.length < 2)strGTm = "0" + strGTm;
	strGTs = (intGT2 % 60).toString();
	if(strGTs.length < 2)strGTs = "0" + strGTs;
	document.getElementById("GameSec").innerHTML = strGTm+":"+strGTs;
	parent.strEndGameTime = intGT - 1;
	intRefreshSec++;
}

function SubmitOddsForm(){
	if(isRefresh == false){
		document.GetOddsForm.action = "http://tw.i5.yimg.com/i/tw/hp/spirit/w5.gif";
		document.GetOddsForm.submit();
		window.status = "";
		return;
	}
	document.GetOddsForm.action = "10m1_odds.php";
	document.GetOddsForm.submit();
	isRefresh = false;
	intRefreshSec = 0;
}

function InitAction(){
	document.GetOddsForm.submit();
	setIntervalID1 = setInterval("SubmitOddsForm();", 30000);
	setIntervalID2 = setInterval("CountRefresh();", 1000);
}

function DisplayWindow(){
	var i = 0;
	score_window.style.display = parent.strDisplay;
	if(parent.strDisplay != "none"){
		for(var i = 0; i < parent.arrImages.length; i++){
			document.getElementById("image" + (i + 1)).src = "../images/lottery/ball/"+parent.arrImages[i]+".gif";
		}
	}
}

function getStakeGold(){
	if(document.getElementById("StakeGold").value == "")return "";
	return eval(document.getElementById("StakeGold").value);
}

function getNumArray(){
	return new Array(1, 21);
}

function SelectOENum(intOE){
	if(intOE != 1 && intOE != 0)return;
	var objGold, intNum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();
	
	for(intNum = arrNum[0]; intNum < arrNum[1]; intNum++){
		if (intNum % 2 == intOE)	{
			objGold = document.ShowOddsForm["Odds1_" + intNum.toString()];
			objGold.value = intGold;
		}
	}
}

function SelectOUNum(intOU){
	if(intOU != 1 && intOU != 0)return;
	var objGold, intNum = 0;
	var MaxNum = 0, MinNum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();

	if(intOU == 1){
		MaxNum = arrNum[1];
		MinNum = parseInt(SpecialOverNumber);
	}else{
		MaxNum = parseInt(SpecialOverNumber) - 1;
		MinNum = 1;
	}
	for(intNum = MinNum; intNum <= MaxNum; intNum++){
		if(intNum >= parseInt(DrawSpecial))continue;
		objGold = document.ShowOddsForm["Odds1_" + intNum.toString()];
		objGold.value = intGold;
	}
}

function SelectSumNum(intOE){
	if(intOE != 1 && intOE != 0)return;
	var objGold, intNum = 0, strNum = "", intLNum = 0, intRNum = 0, intSum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();
	
	for(intNum = arrNum[0]; intNum < arrNum[1]; intNum++){
		strNum = NumFormat(intNum);
	    intLNum = parseInt(strNum.substr(0,1));
	    intRNum = parseInt(strNum.substr(1,1));
	    intSum = intLNum + intRNum;
		if (intSum % 2 == intOE)	{
			objGold = document.ShowOddsForm["Odds1_" + intNum.toString()];
			objGold.value = intGold;
		}
	}
}


function SelectOUOEBall(intOUOE){
	var objGold, intNum = 0;
	var arrNum = getNumArray();
	var intGold = getStakeGold();
	var OverNum = parseInt(SpecialOverNumber);
	
	for(intNum = arrNum[0]; intNum <= arrNum[1]; intNum++){
		var strSelectNum = "";
		switch (intOUOE) {
			case 0: //大單
				if(intNum >= OverNum && intNum % 2 == 1)strSelectNum = intNum.toString();
				break;
			case 1: //大雙
				if(intNum >= OverNum && intNum % 2 == 0)strSelectNum = intNum.toString();
				break;
			case 2: //小單
				if(intNum < OverNum && intNum % 2 == 1)strSelectNum = intNum.toString();
				break;
			case 3: //小雙
				if(intNum < OverNum && intNum % 2 == 0)strSelectNum = intNum.toString();
				break;
			case 4: //尾大
				if((intNum % 10) > 4)strSelectNum = intNum.toString();
				break;
			case 5: //尾小
				if((intNum % 10) < 5)strSelectNum = intNum.toString();
				break;
		}

		if(strSelectNum != ""){
			objGold = document.ShowOddsForm["Odds1_" + intNum.toString()];
			objGold.value = intGold;
		}
	}
}